<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use app\admin\model\Cotype;//公司类型
use app\admin\model\Coattr;//资料属性
use app\admin\model\Company;//资料属性
use app\admin\model\Linkman;//联系人
use app\admin\model\Co_bank;//银行

class Customer extends Auth
{
    //公司类型
    public function customertype(Request $request)
    {
        $res = $request->param();
        if ($request->isAjax() && $request->isPost()) {
            //返回公司类型的数据
            $cotype = new Cotype();
            $getcotype = $cotype->getcotype($res);
            echo json_encode(array('getcotype' => $getcotype['data'], 'size' => $getcotype['count']));
        } elseif ($request->isGet()) {
            return view('cotype/cotype');
        }
    }

    //公司类型的添加
    public function cotypadd(Request $request)
    {
        $data = request()->param();
        $data['lang'] = $this->lang;
        $cotype = new Cotype();
        if ($request->isGet()) {
            return view('cotype/cotypeinfo', [
                'str' => 'cotypadd',
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getcotype'])) {
            $admin = session('admin');
            $admin_countryid = '';
            //不是全球管理员，返回这个职位的国家id
            if ($admin['position_id'] != 1) {
                $admin_countryid = getinfo($admin['position_id'], 'position')['country_id'];
            }
            echo json_encode(array('info' => '', 'country_id' => $admin_countryid));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'cotypadd') {
            $cotyperet = $cotype->addeditcotype($data);
            $udata["status"] = $cotyperet['status'];
            $udata['msg'] = $cotyperet['msg'];
            echo json_encode($udata);
        }
    }

    //公司类型的编辑
    public function cotypedit(Request $request)
    {
        $data = request()->param();
        $data['lang'] = $this->lang;
        $cotype = new Cotype();
        if ($request->isGet()) {
            return view('cotype/cotypeinfo', [
                'str' => 'cotypedit',
                'id' => $data['id'],
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getcotype'])) {
            $admin = session('admin');
            //获取对应的语言的公司类型
            $where['id'] = $data['id'];
            $info = getlanginfo('cotype', $where);
            $admin_countryid = '';
            //不是全球管理员，返回这个职位的国家id
            if ($admin['position_id'] != 1) {
                $admin_countryid = getinfo($admin['position_id'], 'position')['country_id'];
            }
            echo json_encode(array('info' => $info, 'country_id' => $admin_countryid));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'cotypedit') {
            $cotyperet = $cotype->addeditcotype($data);
            $udata["status"] = $cotyperet['status'];
            $udata['msg'] = $cotyperet['msg'];
            echo json_encode($udata);
        }

    }

    //公司类型的删除
    public function cotypdelete(Request $request)
    {
        $cotype = new Cotype();
        $data = $request->param();
        $data['lang'] = $this->lang;
        if ($request->isPost() && $request->isAjax()) {
            $cotyperet = $cotype->delcotype($data);
            $udata["status"] = $cotyperet['status'];
            $udata['msg'] = $cotyperet['msg'];
            echo json_encode($udata);
        }
    }

    //资料属性
    public function customercoattr(Request $request)
    {
        $res = $request->param();
        if ($request->isAjax() && $request->isPost()) {
            $coattr = new Coattr();
            $getcoattr = $coattr->getcoattr($res);
            echo json_encode(array('getcoattr' => $getcoattr['data'], 'size' => $getcoattr['count']));
        } elseif ($request->isGet()) {
            return view('coattr/coattr');
        }
    }

    //属性的添加
    public function coattradd(Request $request)
    {
        $data = request()->param();
        $coattr = new Coattr();
        $data['langs'] = $this->lang;
        if ($request->isGet()) {
            return view('coattr/coattrinfo', [
                'str' => 'coattradd',
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getcoattr'])) {
            //公司类型
            $where['a.lang'] = $data['lang'];//语言标志
            $join = [
                ['nt_cotype b', 'a.mkey=b.mkey']
            ];
            $table = 'multi_lang';
            //获取公司的类型
            $resdata = getmultidata($table, $join, $where)['data'];
            echo json_encode($resdata);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'coattradd') {
            $coattrret = $coattr->addeditcoattr($data);
            $udata["status"] = $coattrret['status'];
            $udata['msg'] = $coattrret['msg'];
            echo json_encode($udata);
        }

    }

    //资料属性的编辑
    public function coattredit(Request $request)
    {
        $data = request()->param();
        $coattr = new Coattr();
        $data['langs'] = $this->lang;
        if ($request->isGet()) {
            return view('coattr/coattrinfo', [
                'str' => 'coattredit',
                'id' => $data['id'],
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getcoattr'])) {
            //获取编辑的数据
            $wheremap['id'] = $data['id'];
            $info = getlanginfo('coattr', $wheremap);
            $options = Db::name('coattr_select')->field('option_text')->where('coattr_id', $info['id'])->select();
            $info['coattr_select'] = array_column($options, 'option_text');
            //公司类型
            $where['a.lang'] = $data['lang'];//语言标志
            $join = [
                ['nt_cotype b', 'a.mkey=b.mkey']
            ];
            $table = 'multi_lang';
            //获取公司的类型
            $info['cotypedata'] = getmultidata($table, $join, $where)['data'];
            echo json_encode($info);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'coattredit') {
            //编辑
            $coattrret = $coattr->addeditcoattr($data);
            $udata["status"] = $coattrret['status'];
            $udata['msg'] = $coattrret['msg'];
            echo json_encode($udata);
        }
    }

    //资料属性的删除
    public function coattrdelete(Request $request)
    {
        $coattr = new Coattr();
        $data = $request->param();
        $data['langs'] = $this->lang;
        if ($request->isPost() && $request->isAjax()) {
            $coattrret = $coattr->delcoattr($data, 1);
            $udata["status"] = $coattrret['status'];
            $udata['msg'] = $coattrret['msg'];
            echo json_encode($udata);
        }
    }

    //资料属性的清除
    public function coattrclear(Request $request)
    {
        $coattr = new Coattr();
        $data = $request->param();
        $data['langs'] = $this->lang;
        if ($request->isPost() && $request->isAjax()) {
            $coattrret = $coattr->delcoattr($data, 2);
            $udata["status"] = $coattrret['status'];
            $udata['msg'] = $coattrret['msg'];
            echo json_encode($udata);
        }
    }

    //客户列表
    public function customercompany(Request $request)
    {
        $res = $request->param();
        $res['lang'] = $this->lang;
        if ($request->isAjax() && $request->isPost()) {
            $company = new Company();
            $getcompany = $company->getcompany($res);
            //国家的显示
            $country = getables('country');
            if (isset($res['country_id']) && $res['country_id']) {
                $provinces = Db:: name('provinces')->where('country_id', $res['country_id'])->select();
            } else {
                $provinces = getables('provinces');
            }
            $join = [
                ['nt_cotype b', 'a.mkey=b.mkey']
            ];
            $table = 'multi_lang';
            $where['a.lang'] = 'zh';//语言标志
            $cotype = getmultidata($table, $join, $where)['data'];
            echo json_encode(array('getcompany' => $getcompany['data'], 'size' => $getcompany['count'], 'dataids' => $getcompany['ids'], 'country' => $country, 'provinces' => $provinces, 'cotype' => $cotype));
        } elseif ($request->isGet()) {
            return view('company/company');
        }
    }

    //处理客户的锁定与批量锁定
    public function companylock(Request $request)
    {
        $data = $request->param();
        $company = new Company();
        if ($request->isPost() && $request->isAjax()) {
            $companyret = $company->ulockadjustrecover($data);
            $udata["status"] = $companyret['status'];
            $udata['msg'] = $companyret['msg'];
            echo json_encode($udata);
        }
    }

    //处理客户的解锁
    public function companyunlock(Request $request)
    {
        $data = $request->param();
        $company = new Company();
        if ($request->isPost() && $request->isAjax()) {
            $companyret = $company->ulockadjustrecover($data);
            $udata["status"] = $companyret['status'];
            $udata['msg'] = $companyret['msg'];
            echo json_encode($udata);
        }
    }

    //处理客户的删除与批量删除（假删除）
    public function companydel(Request $request)
    {
        $data = $request->param();
        $company = new Company();
        if ($request->isPost() && $request->isAjax()) {
            $companyret = $company->ulockadjustrecover($data);
            $udata["status"] = $companyret['status'];
            $udata['msg'] = $companyret['msg'];
            echo json_encode($udata);
        }
    }

    //处理客户的删除与批量删除（真删除）
    public function companyclear(Request $request)
    {
        $data = $request->param();
        $company = new Company();
        if ($request->isPost() && $request->isAjax()) {
            $companyret = $company->ulockadjustrecover($data);
            $udata["status"] = $companyret['status'];
            $udata['msg'] = $companyret['msg'];
            echo json_encode($udata);
        }
    }

    //处理客户的恢复
    public function companyrecover(Request $request)
    {
        $data = $request->param();
        $company = new Company();
        if ($request->isPost() && $request->isAjax()) {
            $companyret = $company->ulockadjustrecover($data);
            $udata["status"] = $companyret['status'];
            $udata['msg'] = $companyret['msg'];
            echo json_encode($udata);
        }
    }

    //处理新客户的添加
    public function companyadd(Request $request)
    {
        $data = $request->param();
        $lang = $data['lang'] = $this->lang;
        $company = new Company();
        if ($request->isGet()) {
            return view('company/companyinfo', [
                'str' => 'companyadd',
                'country_id' => '',//国家id
                'company_id' => '',//公司id
            ]);
        } elseif ($request->isPost() && $request->isAjax() && isset($data['status'])) {
            //编辑点击不同的标签获取不同的数据
            $resdata = $company->getdetail($data);
            if ($data['status'] == 'companyadd') {
                //添加的显示
                echo json_encode($resdata);
            } else {
                //切换标签显示
                echo json_encode(array('data' => $resdata['data'], 'size' => $resdata['count'], 'ids' => $resdata['ids']));
            }
        } elseif ($request->isPost() && $request->isAjax() && $data['act'] == 'companyadd') {
            //新客户提交的接口
            $companyret = $company->addeditcompany($data);
            $udata["status"] = $companyret['status'];
            $udata['msg'] = $companyret['msg'];
            echo json_encode($udata);
        }
    }

    //公司的编辑
    public function companyedit(Request $request)
    {
        $data = $request->param();
        $company = new Company();
        $lang = $data['lang'] = $this->lang;
        if ($request->isGet()) {
            $country_id = Db::name('co_profile')->where('co_id', $data['company_id'])->value('country_id');
            return view('company/companyinfo', [
                'str' => 'companyedit',
                'company_id' => $data['company_id'],//公司id
                'country_id' => $country_id,//国家id
            ]);
        } elseif ($request->isPost() && $request->isAjax() && isset($data['status'])) {
            //编辑点击不同的标签获取不同的数据
            $resdata = $company->getdetail($data);
            if ($data['status'] == 'companyedit') {
                //编辑时显示页面
                $coattrss['cotype'] = $resdata['info']['type_id'];
                $coattrss['lang'] = $lang;
                $textselctname = $company->indocoattrss($coattrss);
                $resdata['info']['typeselect'] = $textselctname;
                echo json_encode($resdata);
            } else {
                //编辑切换标签显示
                echo json_encode(array('data' => $resdata['data'], 'size' => $resdata['count'], 'ids' => $resdata['ids']));
            }
        } elseif ($request->isPost() && $request->isAjax() && $data['act'] == 'companyedit') {
            //编辑提交后的数据接口
            $companyret = $company->addeditcompany($data);
            $udata["status"] = $companyret['status'];
            $udata['msg'] = $companyret['msg'];
            echo json_encode($udata);
        }
    }

    //公司的查看
    public function companycheck(Request $request)
    {
        $data = $request->param();
        $data['lang'] = $this->lang;
        $company = new Company();
        if ($request->isGet()) {
            //通过公司id获取国家id
            $country_id = Db::name('co_profile')->where('co_id', $data['company_id'])->value('country_id');
            return view('company/companydetail', [
                'company_id' => $data['company_id'],//公司id
                'country_id' => $country_id,//国家id
            ]);
        } elseif ($request->isPost() && $request->isAjax()) {
            //详情信息的点击
            $resdata = $company->getdetail($data);
            if ($data['status'] == 'detail') {
                //公司的详情数据的查看
                echo json_encode(array('data' => $resdata['data']));
            } else {
                    echo json_encode(array('data' => $resdata['data'], 'size' => $resdata['count'], 'ids' => $resdata['ids']));
            }
        }
    }

    //处理联系人的添加（查看进入时的添加联系人）
    public function liaisonadd(Request $request)
    {
        $data = $request->param();
        $link = new Linkman();
        if ($request->isGet()) {
            //联系人的添加
            return view('company/liaisoninfo', [
                'liaison' => 'liaisonadd',
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getliaison'])) {
            //获取对应国家的联系方式
            $getmethods = $this->getmethname($data['country_id'], 1);
            echo json_encode(array('info' => '', 'getmethods' => $getmethods));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'liaisonadd') {
            //提交数据
            $linkret = $link->addeditlink($data);
            $udata["status"] = $linkret['status'];
            $udata['msg'] = $linkret['msg'];
            echo json_encode($udata);
        }
    }

    //处理联系人的编辑
    public function liaisonedit(Request $request)
    {
        $data = $request->param();
        $link = new Linkman();
        if ($request->isGet()) {
            return view('company/liaisoninfo', [
                'liaison' => 'liaisonedit',
                'linkid' => $data['linkid'],//联系人id
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getliaison'])) {
            //获取国家的联系方式名称
            $getmethods = $this->getmethname($data['country_id'], 1);
            $info = getinfo($data['linkid'], 'linkman');
            $linkdata = Db::name('linkman_method')->field('coattr_id,content')->where('linkman_id', $data['linkid'])->select();
            foreach ($linkdata as $linkdatakey => $linkdataval) {
                $linkdata[$linkdatakey]['linkname'] = getinfo($linkdataval['coattr_id'], 'coattr_select')['option_text'];
            }
            $info['linkmethods'] = $linkdata;
            echo json_encode(array('info' => $info, 'getmethods' => $getmethods));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'liaisonedit') {
            //提交数据
            $linkret = $link->addeditlink($data);
            $udata["status"] = $linkret['status'];
            $udata['msg'] = $linkret['msg'];
            echo json_encode($udata);
        }
    }

    //处理联系人的删除
    public function liaisondel(Request $request)
    {
        $link = new Linkman();
        $data = $request->param();
        if ($request->isPost() && $request->isAjax()) {
            $linkret = $link->dellink($data);
            $udata["status"] = $linkret['status'];
            $udata['msg'] = $linkret['msg'];
            echo json_encode($udata);
        }
    }

    //处理银行的添加（查看进入时的添加联系人）
    public function bankadd(Request $request)
    {
        $data = $request->param();
        $bank = new Co_bank();
        if ($request->isGet()) {
            //联系人的添加
            return view('company/bankinfo', [
                'bank' => 'bankadd',
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getbank'])) {
            //获取国家的银行
            $getmethods = $this->getmethname($data['country_id'], 2);
            echo json_encode(array('info' => '', 'getmethods' => $getmethods));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'bankadd') {
            //提交数据
            $bankret = $bank->addeditbank($data);
            $udata["status"] = $bankret['status'];
            $udata['msg'] = $bankret['msg'];
            echo json_encode($udata);
        }
    }

    //处理银行的编辑
    public function bankedit(Request $request)
    {
        $data = $request->param();
        $bank = new Co_bank();
        if ($request->isGet()) {
            return view('company/bankinfo', [
                'bank' => 'bankedit',
                'bankid' => $data['bankid'],//银行id
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getbank'])) {
            //获取国家的银行
            $getmethods = $this->getmethname($data['country_id'], 2);
            $info = getinfo($data['bankid'], 'Co_bank');
            echo json_encode(array('info' => $info, 'getmethods' => $getmethods));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'bankedit') {
            //提交数据
            $bankret = $bank->addeditbank($data);
            $udata["status"] = $bankret['status'];
            $udata['msg'] = $bankret['msg'];
            echo json_encode($udata);
        }
    }

    //处理银行的删除
    public function bankdel(Request $request)
    {
        $bank = new Co_bank();
        $data = $request->param();
        if ($request->isPost() && $request->isAjax()) {
            $bankret = $bank->delbank($data);
            $udata["status"] = $bankret['status'];
            $udata['msg'] = $bankret['msg'];
            echo json_encode($udata);
        }
    }

    //公司的批量调整
    public function batchcompany(Request $request)
    {
        $data = $request->param();
        $data['lang'] = $this->lang;
        $company = new Company();
        if ($request->isGet()) {
            return view('company/batchall');
        } else if ($request->isAjax() && $request->isPost() && isset($data['getbatch'])) {
            //获取批量调整后的数据
            $companydata = $company->getcompanydatas($data);
            echo json_encode(array('getcompany' => $companydata));
        } else if ($request->isAjax() && $request->isPost() && $data['act'] == 'batchadd') {
            //提交的数据
            $companyret = $company->batchall($data);
            $udata["status"] = $companyret['status'];
            $udata['msg'] = $companyret['msg'];
            echo json_encode($udata);
        }
    }


    //获取国家的联系方式名称
    protected function getmethname($country_id = '', $code)
    {
        $mapbank = array();
        if ($country_id) {
            $mapbank['country_id'] = $mapcoattr['country_id'] = $country_id;
        }
        $mapcoattr['is_text'] = 1;
        $mapcoattr['person'] = 1;
        $mapcoattr['del_id'] = 0;//未删除
        //获取联系名称
        if ($code == 1) {
            //返回联系名称
            $co_ids = array_column(Db::name('coattr')->field('id')->where($mapcoattr)->select(), 'id');
            $mapselect['coattr_id'] = ['in', $co_ids];
            $data = Db::name('coattr_select')->where($mapselect)->select();
        } else {
            //返回国家银行
            $data = Db::name('banks')->field('id,name')->where($mapbank)->select();
        }
        return $data;
    }

}
